package com.nanyang.tms.manager.supplier.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.supplier.SupplierDao;
import com.nanyang.tms.domain.supplier.SupplierDO;
import com.nanyang.tms.domain.supplier.SupplierQuery;
import com.nanyang.tms.domain.supplier.SupplierVO;
import com.nanyang.tms.manager.supplier.SupplierManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("supplierManager")
public class SupplierManagerImpl implements SupplierManager {
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Override
    public PageBean<SupplierVO> listSupplier(SupplierQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = supplierDao.countSupplier(query);
            PageBean<SupplierVO> page = new PageTableBean<SupplierVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(supplierDao.listSupplier(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

    @Override
    @SystemLog(module =SystemLog.Module.SUPPLIER,template="supplierAdd")
    public void saveSupplier(SupplierDO supplierDO) {
        try {
            SupplierQuery supplierQuery = new  SupplierQuery();
            supplierQuery.setSupplierName(supplierDO.getSupplierName());
            if (supplierDao.queryVerifySupplierName(supplierQuery) > 0) {
                throw new TmsManagerException(TmsExceptionEnum.SUPPLIERNAME_IS_EXIST.getCode(),TmsExceptionEnum.SUPPLIERNAME_IS_EXIST.getMessage());
            }
            supplierDO.setSupplierCode(TmsSerializeConstant.SUPPLIER_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SUPPLIER_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY));
            supplierDao.insert(supplierDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

    @Override
    @SystemLog(module =SystemLog.Module.SUPPLIER,template="supplierUpdate")
    public void updateSupplier(SupplierDO supplierDO) {
        try {
            SupplierQuery supplierQuery = new  SupplierQuery();
            supplierQuery.setSupplierName(supplierDO.getSupplierName());
            supplierQuery.setSupplierCode(supplierDO.getSupplierCode());
            if (supplierDao.queryVerifySupplierName(supplierQuery) > 0) {
                throw new TmsManagerException(TmsExceptionEnum.SUPPLIERNAME_IS_EXIST.getCode(),TmsExceptionEnum.SUPPLIERNAME_IS_EXIST.getMessage());
            }
            supplierDao.update(supplierDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
